package com.dlna.client.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.dlna.client.R;
import com.dlna.client.enity.PictureInfo;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;

public class ImagePagerAdapterV1 extends PagerAdapter {
	private DisplayImageOptions options;
	private ArrayList<PictureInfo> picInfos;

	private LayoutInflater inflater;

	public ImagePagerAdapterV1(Context context, ArrayList<PictureInfo> images) {
		this.picInfos = images;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		options = new DisplayImageOptions.Builder()
				.showImageForEmptyUri(R.drawable.ic_empty)
				.showImageOnFail(R.drawable.ic_error)
				/*.cacheInMemory(true)*/
				.cacheInMemory(true)
				.cacheOnDisc(true)
				.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
				/*.imageScaleType(ImageScaleType.IN_SAMPLE_INT)*/
				.bitmapConfig(Bitmap.Config.RGB_565)
				.build();
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		((ViewPager) container).removeView((View) object);
	}

	@Override
	public void finishUpdate(View container) {
	}

	@Override
	public int getItemPosition(Object object) {
		return POSITION_NONE;
	}

	@Override
	public int getCount() {
		return picInfos.size();
	}

	@Override
	public Object instantiateItem(ViewGroup view, int position) {
		View imageLayout = inflater.inflate(R.layout.item_pager_image, view,
				false);
		ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
		final LinearLayout textLayout = (LinearLayout)imageLayout.findViewById(R.id.textLayout);
		TextView imageTitle = (TextView)imageLayout.findViewById(R.id.imageTitle);
		TextView imageText = (TextView)imageLayout.findViewById(R.id.imageText);
		
		
		final ProgressBar spinner = (ProgressBar) imageLayout
				.findViewById(R.id.loading);

		final PictureInfo Info = picInfos.get(position);
		if(!TextUtils.isEmpty(Info.text)){
			int indexOf = Info.text.indexOf("\n");
			imageTitle.setText(Info.text.substring(0,indexOf));
			imageText.setText(Info.text.substring(indexOf+2));
			imageLayout.setTag(true);
		}else{
			imageLayout.setTag(false);
		}
		String url = "";

		url = "file://" + Info.filePath;
		

		ImageLoader.getInstance().displayImage(url, imageView, options,
				new SimpleImageLoadingListener() {

					@Override
					public void onLoadingStarted(String imageUri, View view) {
						spinner.setVisibility(View.VISIBLE);
					}

					@Override
					public void onLoadingFailed(String imageUri, View view,
							FailReason failReason) {
						spinner.setVisibility(View.GONE);
					}

					@Override
					public void onLoadingComplete(String imageUri, View view,
							Bitmap loadedImage) {
//						if (loadedImage.getHeight() > loadedImage.getWidth()) {
//							ImageView imgview = (ImageView) view;
//							Matrix matrix = new Matrix();
//							matrix.postScale(1f, 1f);
//							matrix.postRotate(-90);
//							imgview.setImageBitmap(Bitmap.createBitmap(
//									loadedImage, 0, 0, loadedImage.getWidth(),
//									loadedImage.getHeight(), matrix, true));
//						} else {
//
//						}
						spinner.setVisibility(View.GONE);
						
//						if(!TextUtils.isEmpty(Info.text)){
//							textLayout.setVisibility(View.VISIBLE);
//						}else{
//							textLayout.setVisibility(View.GONE);
//						}
					}
				});
		((ViewPager) view).addView(imageLayout, 0);
		return imageLayout;
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view.equals(object);
	}

	@Override
	public void restoreState(Parcelable state, ClassLoader loader) {
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	@Override
	public void startUpdate(View container) {
	}
	
	public void upData(ArrayList<PictureInfo> list){
		this.picInfos = list;
		notifyDataSetChanged();
	}
}